// pages/user/userCard/userCard.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userCode: '',
        userInfo: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getUserInfo()
        this.getUserCode()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getUserCode: function() {
        const that = this
        let data = {}
        common.post('QrCodes/app_create_qr', data).then(function(res) {
            that.setData({
                userCode: res
            })
        }).catch(function(e) {})
    },

    getUserInfo: function() {
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    userInfo: {}
                })
            } else {
                that.setData({
                    userInfo: res
                })
            }
        }).catch(function(e) {})
    },

    saveImg: function() {
        const that = this
        wx.getImageInfo({
            src: 'https:' + that.data.userCode.split(':')[1],
            success(res) {
                const ctx = wx.createCanvasContext('shareCanvas')
                ctx.rect(0, 0, 652, 720)
                ctx.setFillStyle('white')
                ctx.fill()
                ctx.drawImage(res.path, 156, 104, 340, 340)
                ctx.setFillStyle('#131313')
                ctx.setFontSize(28)
                ctx.setTextAlign('center')
                ctx.fillText(that.data.userInfo.nickname.slice(0, 5), 326, 520)
                ctx.setFillStyle('#666666')
                ctx.setFontSize(24)
                ctx.setTextAlign('center')
                ctx.fillText('邀请码：' + that.data.userInfo.user_id, 326, 560)
                ctx.setFillStyle('#000000')
                ctx.setFontSize(24)
                ctx.fillText('—— 识别二维码，进入小程序 ——', 326, 620)
                ctx.draw(false, function() {
                    wx.canvasToTempFilePath({
                        width: 652,
                        height: 720,
                        canvasId: 'shareCanvas',
                        success(res) {
                            const saveImg = res.tempFilePath
                            wx.saveImageToPhotosAlbum({
                                filePath: saveImg,
                                success: function(data) {
                                    wx.showToast({
                                        title: '保存成功',
                                        icon: 'success',
                                        duration: 2000
                                    })
                                },
                                fail: function(err) {
                                    if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                                        wx.showModal({
                                            title: '用户未授权',
                                            content: '请先开启保存到相册权限，开启后即可保存图片',
                                            success(res) {
                                                if (res.confirm) {
                                                    wx.openSetting({
                                                        success(settingdata) {
                                                            if (settingdata.authSetting['scope.writePhotosAlbum']) {
                                                                wx.saveImageToPhotosAlbum({
                                                                    filePath: saveImg,
                                                                    success(res) {
                                                                        wx.showToast({
                                                                            title: '保存成功',
                                                                            icon: 'success',
                                                                            duration: 2000
                                                                        })
                                                                    }
                                                                })
                                                            } else {
                                                                wx.showToast({
                                                                    title: '保存图片失败，请确认开启授权后重新保存图片',
                                                                    icon: 'none',
                                                                    duration: 2000
                                                                })
                                                            }
                                                        }
                                                    })
                                                } else if (res.cancel) {
                                                    wx.showToast({
                                                        title: '保存图片失败，请确认开启授权后重新保存图片',
                                                        icon: 'none',
                                                        duration: 2000
                                                    })
                                                }
                                            }
                                        })
                                    }
                                }
                            })
                        }
                    })

                })
            }
        })
    }
})